//
//  TrajectFontColorView.m
//  carcareIOS
//
//  Created by 王然 on 16/7/5.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "TrajectFontColorView.h"
#define triangleW (8)//右侧三角的宽度

@implementation TrajectFontColorView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
- (void)reloadViewWithTitleArr:(NSArray *)titleArr withColorArr:(NSArray *)colorArr
{

    [self removeAllSubviews];
    CGFloat smallVY = 15;
    CGFloat smallVAndVY = 6;
    CGFloat smallVX = 9;
    CGFloat smallVW = self.width - triangleW - smallVX * 2 + 2;
    CGFloat smallVH = 19;
    for (int index = 0 ; index < titleArr.count; index ++) {
        [self createSmallVWithFrame:CGRectMake(smallVX, smallVY + (smallVH + smallVAndVY) * index, smallVW, smallVH) withTitle:titleArr[index] withColor:colorArr[index]];
    }
}

- (UIView *)createSmallVWithFrame:(CGRect)frame withTitle:(NSString *)title withColor:(UIColor *)color
{
    UIView * smallV = [[UIView viewWithFrame:frame] setupOnView:self];
    CGFloat colorVH = 8;
    CGFloat colorVW = 15;
    
    UIView * colorV = [[[[UIView viewWithFrame:CGRectMake(0, (frame.size.height - colorVH) / 2, colorVW, colorVH)] resetBackgroundColor:color] resetBorderWidth:0.5 borderColor:COLOR_HEXSTRING(@"#696969")] setupOnView:smallV];
    colorV.layer.cornerRadius = 1;
    colorV.layer.masksToBounds = YES;
    CGFloat titleLX =  5 + colorV.right;
    [[UILabel labelWithFrame:CGRectMake(titleLX, 0, frame.size.width - titleLX , frame.size.height) font:FONT_DEFAULT_Light(12) text:title textColor:COLOR_HEXSTRING(@"#707070")] setupOnView:smallV];
    return smallV;
    
}
- (void)drawRect:(CGRect)rect
{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor(context, COLOR_HEXSTRING(@"#c1c1c1").CGColor);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGFloat triangleH = 10;
    CGFloat smallLWAndH = 4;

    CGFloat triangleY = 8;
    
    CGContextMoveToPoint(context, self.width - triangleW, smallLWAndH + triangleY + triangleH);
    CGContextAddArcToPoint(context, self.width - triangleW, self.height , self.width - triangleW - smallLWAndH, self.height, smallLWAndH / 2);
    CGContextAddArcToPoint(context, 0 , self.height, 0 , 0, smallLWAndH / 2);
    CGContextAddArcToPoint(context, 0 , 0, self.width - triangleW, 0, smallLWAndH / 2);
    CGContextAddArcToPoint(context, self.width - triangleW , 0, self.width - triangleW, smallLWAndH + triangleY , smallLWAndH / 2);
    CGContextAddLineToPoint(context, self.width - triangleW, smallLWAndH + triangleY);
    CGContextAddArcToPoint(context, self.width, smallLWAndH + triangleY + triangleH / 2, self.width - triangleW, smallLWAndH + triangleY + triangleH, smallLWAndH / 2);
    CGContextAddLineToPoint(context, self.width - triangleW, smallLWAndH + triangleY + triangleH);
    
    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextClosePath(context);
    
    
}

@end
